package com.etv.launcher.ui.actvity.preference.launcher.launcherPreference;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.etv.launcher.R;
import com.etv.launcher.databinding.FragmentPreferenceLauncherBinding;
import com.etv.launcher.ui.actvity.preference.launcher.statusBarPrefenrence.StatusBarPreferenceItemFragment;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class LauncherPreferenceItemFragment extends Fragment {

    private FragmentPreferenceLauncherBinding binding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        binding = FragmentPreferenceLauncherBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.setListener();
    }


    private void setListener(){
        binding.backToPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager =  getParentFragmentManager();
                fragmentManager.popBackStack();
            }
        });

        binding.statusPreference.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LauncherPreferenceItemFragment.this.changeFragment(new StatusBarPreferenceItemFragment());
            }
        });
    }

    /**
     * 跳转fragment
     * @param fragment
     */
    private void changeFragment(Fragment fragment){
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.setCustomAnimations(
                R.anim.slide_from_right,  // 第二个Fragment从右往左进入
                0,  // 第一个Fragment不需要退出动画
                0,  // 返回时第一个Fragment不需要进入动画
                R.anim.slide_to_right   // 返回时第二个Fragment从左往右退出
        );
        transaction.add(R.id.preference_container, fragment);  // 使用add()而不是replace()
        transaction.addToBackStack("tag");
        transaction.commit();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}